import apis from "./systemApi";

export default {
  namespace: "systemManager",
  state: {
    sensitiveLogList: [],
    operateLogList: [],
    operateTypeFilter: [
      { text: "数据源", value: "1" },
      { text: "场景", value: "2" },
      { text: "计划", value: "3" },
    ],
    operateDepFilter: [], // 操作人部门
    backLogList: [],
    backStateFilter: [
      { text: "未通过", value: "-1" },
      { text: "审批通过", value: "1" },
      { text: "待审批", value: "0" },
    ], // 状态列表
    backDepFilter: [], // 归属部门列表,
    backChannelFilter: [], // 推送渠道列表
    messageBackLogList: [], // 消息总线待办事项列表
    messageBacklogLoading: false,
    messDeptList: [],
    auditErrCode: -1, // 消息总线-审批错误码
    messageTotalCount: 0,
  },
  effects: {
    *fetchSensitiveLogList({ payload }, { call, put }) {
      const res = yield call(apis.fetchSensitiveLogList, payload);
      const { errCode, datas = [], totalCount } = res || {};
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { sensitiveLogList: datas },
        });
      }
      return totalCount;
    },
    *exportSensitiveExcel({ payload }, { call }) {
      const res = yield call(apis.exportSensitiveExcel, payload);

      return res;
    },
    // 接口列表查询
    *interFaceList({ payload }, { call }) {
      const res = yield call(apis.interFaceList, payload);
      return res;
    },
    // 导出接口列表
    *exportInterFaceLog({ payload }, { call }) {
      const res = yield call(apis.exportInterFaceLog, payload);
      return res;
    },

    *fetchOperateLogList({ payload }, { call, put }) {
      const res = yield call(apis.fetchOperateLogList, payload);
      const { errCode, datas = [], totalCount } = res || {};
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { operateLogList: datas },
        });
      }
      return totalCount;
    },
    *fetchOperateDepList({ payload }, { call, put }) {
      const res = yield call(apis.fetchOperateDepList, payload);
      const { errCode, datas = [] } = res || {};
      const depFilterArr = [];
      for (let i = 0; i < datas.length; i += 1) {
        depFilterArr.push({
          text: datas[i].ownerDeptName,
          value: `${datas[i].ownerDept}`,
        });
      }
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { operateDepFilter: depFilterArr },
        });
      }
    },
    *fetchOperateTypeList({ payload }, { call, put }) {
      const res = yield call(apis.fetchOperateTypeList, payload);
      const { errCode, datas = [] } = res || {};
      const typeFilterArr = [];
      for (let i = 0; i < datas.length; i += 1) {
        typeFilterArr.push({
          text: datas[i].operateResourceTypeName,
          value: `${datas[i].operateResourceType}`,
        });
      }
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { operateTypeFilter: typeFilterArr },
        });
      }
    },
    *exportOperateExcel({ payload }, { call }) {
      const res = yield call(apis.exportOperateExcel, payload);
      return res;
    },
    // 待办事项
    *fetchBackLogList({ payload }, { call, put }) {
      const res = yield call(apis.fetchBackLogList, payload);
      const { errCode, datas = [], totalCount } = res || {};
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { backLogList: datas },
        });
      }
      return totalCount;
    },
    *fetchBackDepList({ payload }, { call, put }) {
      const res = yield call(apis.fetchBackDepList, payload);
      const { errCode, datas = [] } = res || {};
      const depFilter = [];
      for (let i = 0; i < datas.length; i += 1) {
        depFilter.push({
          text: datas[i].ownerDeptName,
          value: `${datas[i].ownerDept}`,
        });
      }
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { backDepFilter: depFilter },
        });
      }
    },
    *fetchBackChannelList({ payload }, { call, put }) {
      const res = yield call(apis.fetchBackChannelList, payload);
      const { errCode, datas = [] } = res || {};
      const backChannelFilter = [];
      for (let i = 0; i < datas.length; i += 1) {
        backChannelFilter.push({
          text: datas[i].description,
          value: `${datas[i].pushChannelId}`,
        });
      }
      if (errCode === 0) {
        yield put({
          type: "updateState",
          payload: { backChannelFilter },
        });
      }
    },
    *fetchMessageBackLog({ payload }, { call, put }) {
      yield put({
        type: "messageback/loading",
        payload: true,
      });
      const res = yield call(apis.fetchMessageBackLog, payload);
      const { errCode, datas = [], totalCount } = res || {};
      if (errCode === 0) {
        yield put({
          type: "messageCenter/backlog",
          payload: datas,
        });
        yield put({
          type: "messageBack/total",
          payload: totalCount,
        });
      }
      yield put({
        type: "messageback/loading",
        payload: false,
      });
    },
    *fetchMessBackDept({ payload }, { call, put }) {
      const res = yield call(apis.fetchMessBackDept, payload);
      const { errCode, datas = [] } = res || {};
      if (errCode === 0) {
        yield put({
          type: "messageCenter/messBackDept",
          payload: datas,
        });
      }
    },
    *auditMessage({ payload }, { call, put }) {
      const res = yield call(apis.auditMessage, payload);
      const { errCode } = res || {};
      yield put({
        type: "messageCenter/auditErrCode",
        payload: errCode,
      });
    },
  },
  reducers: {
    updateState: (state, { payload }) => ({
      ...state,
      ...payload,
    }),
    "messageCenter/backlog": (state, { payload }) => ({
      ...state,
      messageBackLogList: payload,
    }),
    "messageback/loading": (state, { payload }) => ({
      ...state,
      messageBacklogLoading: payload,
    }),
    "messageCenter/messBackDept": (state, { payload }) => ({
      ...state,
      messDeptList: payload,
    }),
    "messageCenter/auditErrCode": (state, { payload }) => ({
      ...state,
      auditErrCode: payload,
    }),
    "messageBack/total": (state, { payload }) => ({
      ...state,
      messageTotalCount: payload,
    }),
  },
};
